package com.kanas.webak.config.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.inject.Singleton;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kanas.framework.business.BusinessException;
import com.kanas.webak.config.domain.DictType;
import com.kanas.webak.config.domain.DictValue;
import com.kanas.webak.config.repository.DictValueRepository;

@Service("com.kanas.webak.config.service.dictvalueservice")
@Singleton
public class DictValueServiceImpl implements DictValueService{
	@Autowired
	private DictValueRepository dictValueRepository;
	
	@Override
	@Transactional
	public String addNew(DictValue dv) throws Exception {
		DictValue dict = dictValueRepository.findOne(dv.getId());
		if(dict!=null)throw BusinessException.DATA_DUPLICATION;
		if(dv.getParentId()!=null){
			DictValue parent = dictValueRepository.findOne(dv.getParentId());
			dv.setParent(parent);
		}
		Integer sortNum=dictValueRepository.findMaxSortNumber();
		if(sortNum==null){
			sortNum=0;
		}
		dv.setSortNumber(sortNum+1);
		dictValueRepository.save(dv);
		return dv.getId();
	}
	
	@Override
	@Transactional
	public String update(DictValue dv) throws Exception {
		DictValue dict = dictValueRepository.findOne(dv.getOldId());
		if(dict==null)throw BusinessException.DATA_NOT_FOUND;
		dict.setLabel(dv.getLabel());
		dict.setActiveFlag(dv.getActiveFlag());
		dict.setCode(dv.getCode());
		dict.setSortNumber(dictValueRepository.findMaxSortNumber()+1);
		dictValueRepository.save(dict);
		return dv.getOldId();
	}
	
	@Override
	@Transactional
	public DictValue findOne(String id) {
		DictValue dt = dictValueRepository.findOne(id);
		return dt;
	}
	
	
	@Override
	@Transactional
	public List<DictValue> findByDictType(DictType dictType) {
		List<DictValue> DictList=dictValueRepository.findByDictType(dictType);
		for(DictValue dv:DictList ){
			if(dv.getParent()!=null){
				dv.setParentId(dv.getParent().getId());
			}
		}
		return DictList;
	}
	
	@Override
	@Transactional
	public String remove(String dvId) {
		List<DictValue> dvs=new ArrayList<DictValue>();
		dvs=findTreeById(dvId,dvs);
		dictValueRepository.delete(dvs);
		return dvId;
	}
	
	@Override
	@Transactional
	public String removeAll(List<DictValue> dvs) {
		dictValueRepository.delete(dvs);
		return null;
	}
	
	@SuppressWarnings("unchecked")
	@Transactional
	public List<DictValue> findAll() {
		Iterator<DictValue> dts = dictValueRepository.findAll().iterator();
		return toList(dts);
	}
	
	@Override
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public List toList(Iterator its) {
		List list = new ArrayList();
		while(its.hasNext()) {
			list.add(its.next());
		}
		return list;
	}
	
	@Transactional
	private List<DictValue> findTreeById(String id,List<DictValue> dvs){
		DictValue dv=dictValueRepository.findOne(id);
		dvs.add(dv);
		Set<DictValue> dvcs=dv.getChildrens();
		if(dvcs!=null&&dvcs.size()!=0){
			for(DictValue d:dvcs){
				findTreeById(d.getId(),dvs);
			}
		}
		return dvs;
	}
	
	public DictValueRepository getDictValueRepository() {
		return dictValueRepository;
	}

	public void setDictValueRepository(DictValueRepository dictValueRepository) {
		this.dictValueRepository = dictValueRepository;
	}

}
